#include<func.h>
typedef struct filemode{
 char type;
 char power[20];
}filemode_t;
void transmode(mode_t mode,filemode_t* mod);

void ll(char* dirname,int fd){
    DIR* pdir=opendir(dirname);
    char sendll[1000]={0};
    bezero(sendll,1000);
    ERROR_CHECK(pdir,NULL,"opendir");
    chdir(dirname);
    struct dirent* pdirent;
    while((pdirent=readdir(pdir))!=NULL){
        //存储目录信息
        struct stat statbuf;
        //存储时间信息
        struct tm *tmbuf;
        //存储文件类型及权限
        struct filemode mod;
       memset(&mod,0,sizeof(mod));
        transmode(statbuf.st_mode,&mod);
        int ret=stat(pdirent->d_name,&statbuf);
        tmbuf=localtime(&statbuf.st_mtim);
    

        sprintf(sendll,"%c %s %3ld %8s %8s %8ld %2d月 %2d %2d:%2d %s\n",
                mod.type,
                mod.power,
               statbuf.st_nlink,
               getpwuid(statbuf.st_uid)->pw_name,
               getgrgid(statbuf.st_gid)->gr_name,
               statbuf.st_size,
               tmbuf->tm_mon,
               tmbuf->tm_mday,
               tmbuf->tm_hour,
               tmbuf->tm_min,
               pdirent->d_name);
                int len=0;
                len=strlen(sendll);
                send(fd,&len,sizeof(int),0);
                sendn(fd,sendll,len);


    }
   

    }
void transmode(mode_t mode,filemode_t* mod)
{
    switch (mode & S_IFMT) {
        case S_IFBLK:  mod->type='b';  break;
        case S_IFCHR:  mod->type='c';    break;
        case S_IFDIR:  mod->type='d';     break;
        case S_IFIFO:  mod->type='p';    break;
        case S_IFLNK:  mod->type='l';   break;
        case S_IFREG:  mod->type='-';   break;
        case S_IFSOCK: mod->type='s';   break;
        default:       mod->type='u';    break;
    }
    int ugo[3]={0};
    for(int i=0;i<3;i++){
        ugo[2-i]=mode&07;
        mode>>=3;
    }
    for(int i=0;i<3;i++){
        switch(ugo[i]){
            case 1: strcpy(&(mod->power[3*i]),"--x");   break;
            case 2: strcpy(&(mod->power[3*i]),"-w-");   break;
            case 3: strcpy(&(mod->power[3*i]),"-wx");  break;
            case 4: strcpy(&(mod->power[3*i]),"r--");   break;
            case 5: strcpy(&(mod->power[3*i]),"r-x");   break;
            case 6: strcpy(&(mod->power[3*i]),"rw-");    break;
            case 7: strcpy(&(mod->power[3*i]),"rwx");    break;
            default:strcpy(&(mod->power[3*i]),"---");    break;
            }
        }
}
